package com.free.fs.service.impl;

import com.free.fs.mapper.FileMapper;
import com.free.fs.mapper.ScoreMapper;
import com.free.fs.model.FilePojo;
import com.free.fs.model.Score;
import com.free.fs.service.ScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ScoreServiceImpl implements ScoreService {
    @Autowired
    ScoreMapper scoreMapper;

    @Autowired
    FileMapper fileMapper;


    @Override
    public Score queryScoreByFId(Long file_id) {
        return scoreMapper.queryScoreByFId(file_id);
    }

    @Override
    public Boolean addScore(Score score) {
        if(scoreMapper.insert(score) <= 0){
            return false;
        }


        Long fileId = score.getFileId();
        FilePojo filePojo = fileMapper.selectById(fileId);
        //打分之后，修改对应文件对象的 isScored属性
        filePojo.setIsScored(true);
        //添加上传用户
        score.setUploaderId(filePojo.getUploaderId());
        scoreMapper.updateById(score);
        fileMapper.updateById(filePojo);
        return true;
    }
}
